﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnglishAuction.BUS
{
    public class DauGiaBUS
    {
        //private static EnglishAuctionEntities db = new EnglishAuctionEntities();
        public static void Add(DauGia dauGia)
        {
            using (var context = new EnglishAuctionEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                context.DauGias.Add(dauGia);
                context.SaveChanges();
            }
        }

        public static bool CheckBoi50(double gia)
        {
            if (gia % 50000 == 0)
                return true;
            return false;
        }

        public static bool CheckPriceInput(double gia, double giaDuocDau)
        {
            //Kiem tra xem gia dua vao co >= gia co the dau
            if (gia >= giaDuocDau)
                return true;
            return false;
        }

        public static List<DauGia> GetListBySanPhamId(int id)
        {
            List<DauGia> lst = new List<DauGia>();
            using (var context = new EnglishAuctionEntities())
            {
                lst = context.DauGias.Where(d => d.SanPhamId == id).OrderByDescending(d => d.ThoiGian).ToList();
            }

            return lst;
        }

        public static DauGia GetGiaDauMax(int id)
        {
            DauGia dauGia = null;
            using (var context = new EnglishAuctionEntities())
            {
                var giaCaoNhat = context.DauGias.Where(d => d.SanPhamId == id).Max(p => p.GiaHienTai);
                if (giaCaoNhat != null)
                {
                    dauGia = context.DauGias.Where(d => d.SanPhamId == id && d.GiaHienTai == giaCaoNhat).FirstOrDefault();
                }
            }

            return dauGia;
        }

        public static bool Insert(DauGia dauGia)
        {
            bool result = true;
            using (var context = new EnglishAuctionEntities())
            {
                context.DauGias.Add(dauGia);
                context.SaveChanges();
            }

            return result;
        }

        //public static EnglishAuctionEntities db = new EnglishAuctionEntities();

        public static List<DauGia> GetAll(int userId)
        {
            using (var context = new EnglishAuctionEntities())
            {
                //db.Configuration.ProxyCreationEnabled = false;
                return context.DauGias.Where(d => d.NguoiDungId == userId).OrderBy(p => p.ThoiGian.Value).ToList();
            }
        }

        public static List<DauGia> GetListWin(int userId)
        {
            //db.Configuration.ProxyCreationEnabled = false;
            
            List<DauGia> dauGias = new List<DauGia>();
            using (var context = new EnglishAuctionEntities())
            {
                foreach (var item in context.DauGias.Where(d => d.NguoiDungId == userId).ToList())
                {
                    if (item.SanPham.NgayLenSan.Value.AddDays(item.SanPham.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                    {
                        NguoiThangCuoc ntc = context.NguoiThangCuocs.Where(p => p.SanPhamId == item.SanPhamId).FirstOrDefault();
                        //NguoiThangCuoc ntc = NguoiThangCuocBUS.CheckExist(item.SanPhamId.Value);
                        if (ntc != null && ntc.Gia == item.GiaHienTai)
                            dauGias.Add(item);
                    }
                }
            }
            return dauGias;
        }
    }
}